﻿var OrderId;
var PageFlage;
var nowOrderState;
var yuanyin="";
function getPageValue() {
    
    OrderId = utils.getParam("OrderId");
    PageFlage = utils.getParam("Pageflage");
   

    //根据获得的预约信息ID 查询这条预约信息

    getSubscribeDetailedWithDeviceDataBySubscribeId(OrderId);
    //alert(OrderId);
}

function getSubscribeDetailedWithDeviceDataBySubscribeId(orderID) {
	//alert(orderID);
    var getSubscribeDetailedWithDeviceDataBySubscribeId_URL = URL_1+"getSubscribeDetailedWithDeviceDataBySubscribeId"; //
    
    $.ajax({
        url: getSubscribeDetailedWithDeviceDataBySubscribeId_URL,
        type: 'GET',
        dataType: 'json', 
        cache: false,
        data: {
            subscribe_id: orderID
        },
        beforeSend: LoadFunction, //加载执行方法
        error: erryFunction, //错误执行方法
        success: succFunction //成功执行方法
    })

    function LoadFunction() {
       // $(".header1").html('加载中...');
    }

    function erryFunction() {
        alert("获取-预约详情信息-超时");
    }

    function succFunction(JsonString) {
 
        json = eval(JsonString);
       
        if (json.code > 0) {

            var dataJson = json.extend.getSubscribeDetailedWithDeviceDataBySubscribeId;
            var dataJson_huoban =json.extend.subscribeSon;
            for (var i = 0; i < dataJson.length; i++) {
                //解析JSON
                var id = dataJson[i]["id"];
               
                var subscribeStatus = dataJson[i]["subscribeStatus"];
                nowOrderState= subscribeStatus;
                var subscribeStatusValue = "";
                
                var subscribeBeginTime = dataJson[i]["subscribeBeginTime"];
                var subscribeEndTime = dataJson[i]["subscribeEndTime"];
 		var bbzz= dataJson[i]["subscribeRemark"];
                var deviceType = dataJson[i]["device"]["deviceType"];
                var deviceNumber = dataJson[i]["device"]["deviceNumber"];
                var devicePlace = dataJson[i]["device"]["devicePlace"];
                var subscribeUserName= dataJson[i]["subscribeUserName"];
                var oderfriends;////////////////////////////////////////////////////////////////////////////////
       
                if (subscribeStatus == 1 || subscribeStatus == 2) {
              
                    subscribeStatusValue = "进行中";
                    var stopButton_obj = document.getElementById("stopButton");  
                    var reserveButton_obj = document.getElementById("reserveButton");  
                     var stopButton_obj_lala = document.getElementById("stopButton_lala");  
                    var reserveButton_obj_lala = document.getElementById("reserveButton_lala");  
                    if (PageFlage == "MyOrderPage") {
                      
                            var nowTime = new Date(); //系统当前时间
                            //经过方法转换为毫秒
                            nowTime = fmtMillisecond(nowTime);
                            //后面的数据直接用毫秒
                            var startTime = subscribeBeginTime;
                            var endTime = subscribeEndTime;
                            if (startTime < nowTime && (endTime > nowTime)) { //判断 预约开始时间小于系统当前时间
                                stopButton_obj.style.cssText ="display:block;" //显示终止按钮
                                stopButton_obj_lala.style.cssText ="display:block;" //显示终止按钮
                            } else if (startTime > nowTime ||startTime == nowTime) { //判断 预约开始时间大于等于系统当前时间
                                reserveButton_obj.style.cssText ="display:block;"//显示撤销按钮
                                 reserveButton_obj_lala.style.cssText ="display:block;"//显示撤销按钮
                            } else if (endTime < nowTime ){//结束时间小于系统当前时间
                                stopButton_obj.style.cssText ="display:none;"
                                reserveButton_obj.style.cssText ="display:none;"
                                 stopButton_obj_lala .style.cssText ="display:none;"
                                reserveButton_obj_lala .style.cssText ="display:none;"
                            } 
                       
                      } else if (PageFlage == "EquipmentDetailedPage") {

                            stopButton_obj.style.cssText ="display:none;"
                            reserveButton_obj.style.cssText ="display:none;"
                      }
                } else if (subscribeStatus == 3) {
                    subscribeStatusValue = "已结束";

                } else if (subscribeStatus == 4 ) {
                    subscribeStatusValue = "已撤销";
  
                }else if (subscribeStatus == 5 ) {
                    subscribeStatusValue = "已终止";
    
                }
            }

            //渲染数据
            
            document.getElementById('orderUserName').innerHTML = subscribeUserName;
            document.getElementById('orderState').innerHTML = subscribeStatusValue;
            document.getElementById('startTime').innerHTML = fmtDate(subscribeBeginTime,subscribeEndTime);
            //document.getElementById('endTime').innerHTML = fmtDate(subscribeEndTime);
            document.getElementById('orderEquipment').innerHTML = deviceType;
            document.getElementById('deviceNumber').innerHTML = deviceNumber;
            document.getElementById('equipmentPoint').innerHTML = devicePlace;
            document.getElementById('bbzz').innerHTML = bbzz;

            var htmlString = "";
            if(dataJson_huoban.length>0){
           
                for (var i = 0; i < dataJson_huoban.length; i++) {
                   var id = dataJson_huoban[i].id;
                  var subscribeUserStatus = dataJson_huoban[i].subscribeUserStatus;
                if (PageFlage == "EquipmentDetailedPage") {
                

     			  htmlString+=" <li class=\"mui-table-view-cell\">"+
                    "<div style=\"width: 90%;\"><div style=\"text-align: center; width: 33%; float: left;\">"+ 
                     dataJson_huoban[i].subscribeUserName+"</div><div style=\"text-align: center;width: 33%;float: left; \">"+dataJson_huoban[i].subscribeUserMobile+"</div>"   
                
                if(dataJson_huoban[i].subscribeUserStatusName=="接受"){
                    htmlString+="<div style=\"text-align: center; width: 33%; float: left;color: green;\">"+
                      dataJson_huoban[i].subscribeUserStatusName+"</div></div></li>"
                    }else{
                         htmlString+="<div style=\"text-align: center; width: 33%; float: left;color: red;\">"+
                      dataJson_huoban[i].subscribeUserStatusName+"</div></div></li>"
                    }
                  }else{
                        htmlString+=" <li class=\"mui-table-view-cell\"><div class=\"mui-slider-right mui-disabled\"><a class=\"mui-btn mui-btn-red\" href=\"javascript:chexiaoyuanyin(\'"+id+"\',"+subscribeUserStatus+")\">删除</a></div>"+
                    "<div class=\"mui-slider-handle\"><div style=\"width: 90%;\"><div style=\"text-align: center; width: 33%; float: left;\">"+
                     dataJson_huoban[i].subscribeUserName+"</div><div style=\"text-align: center;width: 33%;float: left;\">"+dataJson_huoban[i].subscribeUserMobile+"</div>"   
              		 if(dataJson_huoban[i].subscribeUserStatusName=="接受"){
                    htmlString+="<div style=\"text-align: center; width: 33%; float: left;color: green;\">"+
                      dataJson_huoban[i].subscribeUserStatusName+"</div></div><img  class=\"mui-pull-right\" src=\"../img/zuohua.png\" style=\"width: 4%;height: 4%\">"+
                    "</div></li>"
                    }else{
                         htmlString+="<div style=\"text-align: center; width: 33%; float: left;color: red;\">"+
                      dataJson_huoban[i].subscribeUserStatusName+"</div></div><img  class=\"mui-pull-right\" src=\"../img/zuohua.png\" style=\"width: 4%;height: 4%\">"+
                    "</div></li>"
                    }
                }
                
             }
                //alert(htmlString.toString().length);
             if(htmlString.toString().length <= 0){
                
            	htmlString ="<div class='nodata col-xs-12 col-sm-12'>暂 无 数 据</div>";	
            }
            
            
            
            }else{
                htmlString ="<li class=\"mui-table-view-cell\"><div class='nodata col-xs-12 col-sm-12'style=\"text-align:center;color:#848587\">无邀约的伙伴</div></li>";
              //document.getElementById('huobanhuoban').innerHTML=htmlString;	
            }
            document.getElementById('huobanhuoban').innerHTML=htmlString;
        } else {
            alert(json.msg);
        }
    }
};
function chexiaoyuanyin(InviteId,subscribeUserStatus){
if(nowOrderState==3 ||nowOrderState==4||nowOrderState==5){
        window.native.toast("当前预约已结束");
       }else{
        if(subscribeUserStatus ==3){
         window.native.toast("已删除");
        }else if(subscribeUserStatus ==2){
         window.native.toast("对方已拒绝邀请，不可删除");
        }else{
        
          var btnArray = ['取消', '确定'];
   		  mui.prompt('请输入您的删除原因：', '原因', '确认删除吗？', btnArray, function(e) {
                    if (e.index == 1) {
                        //mui.toast( e.value);
                       
                        yuanyin = e.value;
                        //alert(yuanyin);
    updateSubscribeSonByIdForUserStatus_click(InviteId,subscribeUserStatus,yuanyin);
                    } else {
                        //mui.toast('你点了取消按钮');
                    }
                })
                
        
        }
}
}

//根据信息id，原因删除受邀人员
function updateSubscribeSonByIdForUserStatus_click(InviteId,subscribeUserStatus,yuanyin){

 //URL拼接
       if(nowOrderState==3 ||nowOrderState==4||nowOrderState==5){
        window.native.toast("当前预约已结束");
       }else{
        if(subscribeUserStatus ==3){
         window.native.toast("已删除");
        }else{
           var updateSubscribeSonByIdForUserStatus_URL = URL_1+"updateSubscribeSonByIdForUserStatus";
    $.ajax({
        url: updateSubscribeSonByIdForUserStatus_URL,
        type: 'GET',
        dataType: 'json',
        cache: false,
        data:{   
            id:InviteId,
            subscribe_user_status:3,
            subscribe_result:yuanyin
        },
        beforeSend: LoadFunction, //加载执行方法
        error: erryFunction, //错误执行方法
        success: succFunction //成功执行方法
    })

    function LoadFunction() {
        $(".header1").html('加载中...');
    }

    function erryFunction() {
        window.native.toast("获取-删除邀约人员-超时");
    }

    function succFunction(JsonString) {
        json = eval(JsonString);
        if (json.code > 0) {
            window.native.toast("删除成功");
            history.go(0);
        } else {
            window.native.toast(json.msg);
        }
    }
                
        }
        }
}

function ButtonOnClick(flage) {
    if (flage == 1) { //终止按钮
        //弹出一个询问框，有确定和取消按钮
        //利用对话框返回的值 （true 或者 false）

         var btnArray = ['否', '是'];
                mui.confirm('是否终止？', '提醒', btnArray, function(e) {
                    if (e.index == 1) {
                        UpdateOrderState(flage,"");
                         
                    } else {
                       mui.toast('你点了否按钮');
                    }
                })

    } else if (flage == 2) { //撤销按钮


    	  var btnArray = ['取消', '确定'];
   		  mui.prompt('请输入您的撤销原因：', '原因', '确认撤销吗？', btnArray, function(e) {
                    if (e.index == 1) {
                        //mui.toast( e.value);
                        UpdateOrderState(flage,e.value);
    
                    } else {
                        //mui.toast('你点了取消按钮');
                    }
                })

    }
}



function UpdateOrderState(flage,Reason) {

    var state;
    if (flage == 1) {
        state = 5; //代表终止状态
    } else if (flage == 2) {
        state = 4; //代表撤销状态
    }
    var UpdateOrderState_URL = URL_1+"updateSubscribeStatusById";

    $.ajax({
        url: UpdateOrderState_URL,
        type: 'GET',
        dataType: 'json',
        cache: false,
        data: {
            subscribe_id: OrderId,
            subscribe_status: state,
            subscribe_result:Reason
        },
        beforeSend: LoadFunction, //加载执行方法
        error: erryFunction, //错误执行方法
        success: succFunction //成功执行方法
    })

    function LoadFunction() {
        //$(".header1").html('加载中...');
    }

    function erryFunction() {
      if (flage == 1) {
          	alert("请求-终止预约信息-超时");
      } else if (flage == 2) {
            alert("请求-撤销预约信息-超时");
      }

    }

    function succFunction(JsonString) {


        json = eval(JsonString);

        if (json.code > 0) {
          if (flage == 1) {
               window.native.toast("终止成功");//代表终止状态
                if(history.length>1){
		history.go(-2);
		}else{
		window.location.href = "MyOrder.html";
		}
                  //self.location=document.referrer;
          } else if (flage == 2) {
                window.native.toast("撤销成功"); //代表撤销状态
		//alert(history.length);
		if(history.length>1){
		history.go(-2);
		}else{
		window.location.href = "MyOrder.html";
		}
                 //self.location=document.referrer;
          }
        } else {
            alert(json.msg);
        }
    }

}

/*将时间字符串格式转换为毫秒直接与json中的参数进行比较判断*/
/*用于时间数据校验*/
function fmtMillisecond(date){
	//将时间字符串格式转换为毫秒
    var y = date.getFullYear();
    //从日期字符串中获取的月份数据再加1
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
   	var time = y+'/'+m+'/'+d+' '+h+':'+minute+':'+second;
   	var t = new Date(time).getTime();
   	return t;
}



/*毫秒转换成正常时间格式*/
/*只用于预约详情显示*/
/*12月1日 星期五  17:00~18:00*/
function fmtDate(stime,etime) {
    var startTime = new Date(stime);
    var endTime = new Date(etime);
    var weekStr = "星期" + "日一二三四五六".charAt(startTime.getDay());
//  var y = date.getFullYear();
    var m = startTime.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = startTime.getDate();
    d = d < 10 ? ('0' + d) : d;
    var startHour = startTime.getHours();
    startHour = startHour < 10 ? ('0' + startHour) : startHour;
    var startminute = startTime.getMinutes();
    startminute = startminute < 10 ? ('0' + startminute) : startminute;
    
    var endHour = endTime.getHours();
    endHour = endHour < 10 ? ('0' + endHour) : endHour;
    var endminute = endTime.getMinutes();
    endminute = endminute < 10 ? ('0' + endminute) : endminute;
    
    var result = m + '-' + d + ' ' +weekStr +" "+ startHour + ':' + startminute + '~'
    + endHour +":"+ endminute;
    return result;
};




// function compareTime(startDate, endDate) {
//  if (startDate.length > 0 && endDate.length > 0) {
//     var startDateTemp = startDate.split(" ");
//     var endDateTemp = endDate.split(" ");
//
//     var arrStartDate = startDateTemp[0].split("-");
//     var arrEndDate = endDateTemp[0].split("-");
//
//     var arrStartTime = startDateTemp[1].split(":");
//     var arrEndTime = endDateTemp[1].split(":");
//
// var allStartDate = new Date(arrStartDate[0], arrStartDate[1], arrStartDate[2], arrStartTime[0], arrStartTime[1], arrStartTime[2]);
// var allEndDate = new Date(arrEndDate[0], arrEndDate[1], arrEndDate[2], arrEndTime[0], arrEndTime[1], arrEndTime[2]);
//
// if (allStartDate.getTime() >= allEndDate.getTime()) {
//         alert("startTime不能大于endTime，不能通过");
//         return false;
// } else {
//     alert("startTime小于endTime，所以通过了");
//     return true;
//        }
// } else {
//     alert("时间不能为空");
//     return false;
//       }
// }
